<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/27
 * Time: 13:47
 */

class SMSCode
{
    /**
     * 获取验证码
     * @param int    $tempID  模板id
     * @param string phone
     * @param
     * @return array|\think\Response
     */
    public function getCode($tmpId,$type)
    {
        $phone      = $_SESSION['phone'];
        $res        = $this->sendCode($phone,$tmpId);
        if (empty($res) || empty($res['code'])) {
            return response(null,'-22','获取验证码信息出错');
        }
        if (strtolower($res['ret']) !== strtolower('OK')) {
            return response(null,'-18',$this->getErrorMessage($res['ret']));
        } else if (strtolower($res['ret']) == strtolower('OK')) {
            //获取到验证码 入库
            $code   = $res['code'];
            $phone  = $res['phone'];
            $flag   = parent::addVerify($phone,$code,$type);
            if ($flag>0) {
                return response(['code'=>$code],'200','获取验证码成功');
            } else {
                return response(null,'-23','获取验证码失败');
            }
        }
    }




    public function sendCode($phone,$tempId=2,$accId = 'LTAIQPsfjWlfLcGD',$accKey='ME8QW2p4z4EvCkqux5Of2pPXj3kMr5',$sign='归仁商城')
    {
        // 0信息变更验证码 1修改密码验证码 2用户注册验证码  3登录异常验证码  4登录确认验证码  5短信测试   6身份验证验证码  7.SMS_112465127 提现
        $tempArr = ['SMS_92695050', 'SMS_92695051', 'SMS_92695052', 'SMS_92695053', 'SMS_92695054', 'SMS_92695055', 'SMS_92695056', 'SMS_112465127'];

        $demo = new SmsDemo(
            $accId,
            $accKey
        );

        $code = (string)rand(1000, 9999);

        $response = $demo->sendSms(
            $sign, // 短信签名
            $tempArr[$tempId], // 短信模板编号
            $phone, // 短信接收者
            Array(  // 短信模板中字段的值
                "code"      => $code,
                "product"   => "dsd"
            ),
            "123"
        );
        $res = ['code'=>$code,'phone'=>$phone,'ret'=>$response->Code];
        var_dump($response);
        return $res;
    }

    public function getErrorMessage($status){
        $errCode = [
            'isp.RAM_PERMISSION_DENY'                       =>	'RAM权限DENY',
            'isv.OUT_OF_SERVICE' 	                        =>  '业务停机',
            'isv.PRODUCT_UN_SUBSCRIPT'                      =>	'未开通云通信产品的阿里云客户',
            'isv.PRODUCT_UNSUBSCRIBE'                       =>	'产品未开通',
            'isv.ACCOUNT_NOT_EXISTS' 	                    =>  '账户不存在',
            'isv.ACCOUNT_ABNORMAL' 	                        =>  '账户异常',
            'isv.SMS_TEMPLATE_ILLEGAL'                      => 	'短信模板不合法',
            'isv.SMS_SIGNATURE_ILLEGAL'                     =>	'短信签名不合法',
            'isv.INVALID_PARAMETERS' 	                    =>  '参数异常',
            'isp.SYSTEM_ERROR'                              =>	'系统错误',
            'isv.MOBILE_NUMBER_ILLEGAL'                     =>	'非法手机号',
            'isv.MOBILE_COUNT_OVER_LIMIT'                   =>	'手机号码数量超过限制',
            'isv.TEMPLATE_MISSING_PARAMETERS'               =>	'模板缺少变量',
            'isv.BUSINESS_LIMIT_CONTROL'                    =>	'业务限流',
            'isv.INVALID_JSON_PARAM'                        =>	'JSON参数不合法，只接受字符串值',
            'isv.BLACK_KEY_CONTROL_LIMIT'                   =>	'黑名单管控',
            'isv.PARAM_LENGTH_LIMIT' 	                    =>  '参数超出长度限制',
            'isv.PARAM_NOT_SUPPORT_URL'                     => 	'不支持URL',
            'isv.AMOUNT_NOT_ENOUGH' 	                    =>  '账户余额不足',
        ];
        if (isset ( $errCode [$status] )) {
            return $errCode [$status];
        }
        return '发送短信失败';
    }








}